const CFG = require('../config');
const mysql = require('mysql');

// 连接池操作数据库
module.exports = class Model
{
  // 连接对象
  static pool = null;
  static first = true;

  static pool_conn()//连接一次就行
  {
    if (this.first) {
      this.pool = mysql.createPool(CFG.MySql);
      this.first = false;
    }
  }//end pool_conn()

  static query(sql, params=[])
  {
    this.pool_conn();
    return new Promise((resolve,reject)=>{
      this.pool.getConnection((err,conn)=>{
        if(err){
          reject(err);
        } else {
          conn.query(sql,params,(err,results)=>{
            if(err) reject(err);
            else resolve(results);
          })
          conn.release();
        }
      })//end Model.pool...
    })
  }//end query()
}